package com.guan.ordering_sys.generator.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guan.ordering_sys.common.PageResult;
import com.guan.ordering_sys.common.ResultApi;
import com.guan.ordering_sys.common.StateCode;
import com.guan.ordering_sys.generator.service.OrderDetailService;
import com.guan.ordering_sys.generator.service.OrderService;
import com.guan.ordering_sys.vo.DingdanVo;
import com.guan.ordering_sys.vo.SingleCheckVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 韩思琪
 * @since 2021-06-18
 */
@Api(tags= "订单控制器")
@RestController
@RequestMapping("/generator/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    OrderDetailService orderDetailService;

    @ApiOperation(value ="订单列表")
    @PostMapping("/orderList")
    public ResultApi orderList(@ApiParam("当前页") @RequestParam(required = true) int pageNum,
                               @ApiParam("页面大小") @RequestParam(required = true) int pageSize){
        Page<DingdanVo>  dingdanVoPage = new Page<>();
        dingdanVoPage.setCurrent(pageNum);
        dingdanVoPage.setSize(pageSize);

        IPage<DingdanVo> dingdanVoIPage = orderService.orderList(dingdanVoPage);
        PageResult<DingdanVo> dingdanVoPageResult = new PageResult<>(dingdanVoIPage.getRecords().size(), dingdanVoIPage.getRecords());
        return ResultApi.success(dingdanVoPageResult);
    }

    @ApiOperation(value ="单个结单")
    @PostMapping("/singleCheck")
    public ResultApi singleCheck(@ApiParam("订单Id") @RequestParam(required = true) int orderId){
        SingleCheckVo singleCheckVo = orderService.getSingleCheck(orderId);
        if (singleCheckVo != null) {
            return ResultApi.success(singleCheckVo);
        } else {
            return ResultApi.errorNotData(StateCode.getApiCode(500));
        }
    }

    @ApiOperation(value ="结账")
    @PostMapping("/updateCheck")
    public ResultApi updateCheck(@ApiParam("订单Id") @RequestParam(required = true) int orderId,
                                 @ApiParam("付款方式") @RequestParam(required = true) String payType){
        if (orderService.updateCheck(orderId,payType) == 1) {
            return ResultApi.successDone(StateCode.getApiCode(200));
        } else {
            return ResultApi.errorNotData(StateCode.getApiCode(500));
        }
    }

}

